/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * SpelerToevoegen.java
 *
 * Created on 13-dec-2011, 11:26:36
 */
package view;

import javax.swing.JOptionPane;
import model.Masterclass;
import model.Speler;

/**
 *
 * @author tomkeim
 */
public class MasterclassWijzigen extends javax.swing.JFrame {

    private Hoofdscherm hscherm;
    /** Creates new form SpelerToevoegen */
    Masterclass eenClass = new Masterclass();
    protected Speler[] spelers;

    public MasterclassWijzigen() {
        initComponents();



    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        iDField = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        kostenField = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        datumField = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        minRatingField = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        searchButton = new javax.swing.JButton();
        searchField = new javax.swing.JTextField();
        LeraarComboBox = new javax.swing.JComboBox();
        confirmButton = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Masterclass wijzigen");
        setLocationByPlatform(true);

        jLabel1.setText("Masterclass ID");

        iDField.setEditable(false);
        iDField.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                iDFieldActionPerformed(evt);
            }
        });

        jLabel2.setText("Leraar");

        jLabel3.setText("Kosten");

        jLabel4.setText("Datum");

        jLabel5.setText("Minimum Rating");

        searchButton.setText("Zoeken");
        searchButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                searchButtonActionPerformed(evt);
            }
        });

        searchField.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                searchFieldActionPerformed(evt);
            }
        });

        LeraarComboBox.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                LeraarComboBoxActionPerformed(evt);
            }
        });

        confirmButton.setText("Confirm");
        confirmButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                confirmButtonActionPerformed(evt);
            }
        });

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                        .add(searchField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 75, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .add(26, 26, 26)
                        .add(searchButton)
                        .add(20, 20, 20))
                    .add(layout.createSequentialGroup()
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                            .add(jLabel3)
                            .add(jLabel4)
                            .add(jLabel5))
                        .add(18, 18, 18)
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                            .add(minRatingField, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 384, Short.MAX_VALUE)
                            .add(datumField, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 384, Short.MAX_VALUE)
                            .add(kostenField, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 384, Short.MAX_VALUE))
                        .addContainerGap())
                    .add(layout.createSequentialGroup()
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                            .add(jLabel1)
                            .add(jLabel2))
                        .add(18, 18, 18)
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                            .add(iDField, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 388, Short.MAX_VALUE)
                            .add(LeraarComboBox, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 149, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                        .addContainerGap())))
            .add(layout.createSequentialGroup()
                .add(171, 171, 171)
                .add(confirmButton)
                .addContainerGap(256, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(searchButton)
                    .add(searchField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .add(18, 18, 18)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jLabel1)
                    .add(iDField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 38, Short.MAX_VALUE)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jLabel2)
                    .add(LeraarComboBox, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .add(34, 34, 34)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jLabel3)
                    .add(kostenField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jLabel4)
                    .add(datumField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jLabel5)
                    .add(minRatingField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .add(7, 7, 7)
                .add(confirmButton)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

private void searchButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_searchButtonActionPerformed
    // Get the search bar information
    try {
        String id = searchField.getText();
        if (id.trim().length() == 0) {
           
            JOptionPane.showMessageDialog(null,"Voer een masterclass ID in");
        }
        else
        {
          
            int MasterID = Integer.parseInt(id);

            eenClass = Masterclass.getMasterclass(MasterID);
            System.out.println(eenClass);
            // S-out will check if info is found, for testing purposes only.

            // TODO add your handling code here:
            // Get all info
            if (Masterclass.exists(MasterID) == true) {
            int masterid = eenClass.getMasterclassID();
            String Stringmasterid = String.valueOf(masterid);

            double kosts = eenClass.getKosten();
            String Stringkosts = String.valueOf(kosts);

            eenClass.getDatumTijd();
            String Stringdatumtijd = eenClass.getDatumTijd();

            int minrating = eenClass.getMinimaleRating();
            String Stringminrating = String.valueOf(minrating);


            // Leraar wordt opgehaald.
            spelers = Speler.getSpelers();
            int selected_index = 0;
            for (int i = 0; i < spelers.length; i++) {
                if (spelers[i].getSpelerid() == eenClass.getLeraar().getSpelerid()) {
                    selected_index = i;
                }
                LeraarComboBox.addItem(spelers[i]);
            }

            LeraarComboBox.setSelectedIndex(selected_index);
           
            // Add search data to text fields so it will be editeable!
            // Velden invullen
            iDField.setText(Stringmasterid);
            kostenField.setText(Stringkosts);
            datumField.setText(Stringdatumtijd);
            minRatingField.setText(Stringminrating);
            }
            else
            {
                JOptionPane.showMessageDialog(null, "De opgevraagde Masterclass met het ID: " + MasterID + " bestaat niet.");
            }
            
        }
    } catch (NumberFormatException e) {
       JOptionPane.showMessageDialog(null, "Een Masterclassid bestaat alleen uit cijfers");

    }
}//GEN-LAST:event_searchButtonActionPerformed

private void searchFieldActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_searchFieldActionPerformed
    //eenClass.leraar =
    // TODO add your handling code here:
}//GEN-LAST:event_searchFieldActionPerformed

private void iDFieldActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_iDFieldActionPerformed
// TODO add your handling code here:
}//GEN-LAST:event_iDFieldActionPerformed

private void LeraarComboBoxActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_LeraarComboBoxActionPerformed
// TODO add your handling code here:
}//GEN-LAST:event_LeraarComboBoxActionPerformed

private void confirmButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_confirmButtonActionPerformed
// TODO add your handling code here:
     try {
         int confirmer = JOptionPane.showConfirmDialog(null, "Weet u zeker dat de waardes kloppen?");
         if (confirmer == JOptionPane.YES_OPTION) {
             
         
         
        Speler leraar = (Speler) LeraarComboBox.getSelectedItem();
         System.out.println(leraar);
        eenClass.setLeraar(leraar);

        double updateKosten = Double.parseDouble(kostenField.getText());
        eenClass.setKosten(updateKosten);

        eenClass.setDatumTijd(datumField.getText());

        int updateMinRating = Integer.parseInt(minRatingField.getText());
        eenClass.setMinimaleRating(updateMinRating);

        
         System.out.println(leraar.getSpelerid());
        eenClass.update();
        JOptionPane.showMessageDialog(null,"De gegevens zijn gewijzigd");
         }
    } catch (NumberFormatException e) {
        JOptionPane.showMessageDialog(null, "Foute invoer.");
    } 


}//GEN-LAST:event_confirmButtonActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(MasterclassWijzigen.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(MasterclassWijzigen.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(MasterclassWijzigen.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(MasterclassWijzigen.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new MasterclassWijzigen().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JComboBox LeraarComboBox;
    private javax.swing.JButton confirmButton;
    private javax.swing.JTextField datumField;
    private javax.swing.JTextField iDField;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JTextField kostenField;
    private javax.swing.JTextField minRatingField;
    private javax.swing.JButton searchButton;
    private javax.swing.JTextField searchField;
    // End of variables declaration//GEN-END:variables
}
